package org.tinygroup.bizframe.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.tinygroup.bizframe.basedao.util.PageResponseAdapter;
import org.tinygroup.bizframe.business.inter.SysDepBusiness;
import org.tinygroup.bizframe.common.dto.PageRequest;
import org.tinygroup.bizframe.common.dto.PageResponse;
import org.tinygroup.bizframe.common.util.BeanUtil;
import org.tinygroup.bizframe.common.util.CamelCaseUtil;
import org.tinygroup.bizframe.dao.inter.pojo.TsysDep;
import org.tinygroup.bizframe.service.inter.SysDepService;
import org.tinygroup.bizframe.service.inter.dto.SysDepDto;
import org.tinygroup.bizframe.service.inter.dto.TreeDto;
import org.tinygroup.commons.tools.StringUtil;
import org.tinygroup.jdbctemplatedslsession.daosupport.OrderBy;
import org.tinygroup.logger.Logger;
import org.tinygroup.logger.LoggerFactory;
import org.tinygroup.tinysqldsl.Pager;

/**
 * Created by Mr.wang on 2016/7/14.
 */
public class SysDepServiceImpl implements SysDepService {

    private static final Logger logger = LoggerFactory.getLogger(SysDepServiceImpl.class);

    private SysDepBusiness sysDepBusiness;

    public SysDepBusiness getSysDepBusiness() {
        return sysDepBusiness;
    }

    public void setSysDepBusiness(SysDepBusiness sysDepBusiness) {
        this.sysDepBusiness = sysDepBusiness;
    }

    public SysDepDto getSysDep(String depCode) {
        return BeanUtil.copyProperties(SysDepDto.class, sysDepBusiness.getById(depCode));
    }

    public SysDepDto addSysDep(SysDepDto sysDepDto) {
        TsysDep tsysdep = BeanUtil.copyProperties(TsysDep.class, sysDepDto);
        return BeanUtil.copyProperties(SysDepDto.class, sysDepBusiness.add(tsysdep));
    }

    public int updateSysDep(SysDepDto sysDepDto) {
        TsysDep tsysdep = BeanUtil.copyProperties(TsysDep.class, sysDepDto);
        return sysDepBusiness.update(tsysdep);
    }

    public int deleteSysDep(String[] depCode) {
        return sysDepBusiness.deleteByKeys(depCode);
    }

    public PageResponse getSysDepPager(PageRequest pageRequest, SysDepDto sysDepDto) {
        TsysDep tsysdep = BeanUtil.copyProperties(TsysDep.class, sysDepDto);

        String sortField = pageRequest.getSort();
        if (StringUtil.isEmpty(sortField)) {
            sortField = CamelCaseUtil.getFieldName("depCode");
        }
        String orderByField = CamelCaseUtil.getFieldName(sortField);
        boolean isAsc = "asc".equalsIgnoreCase(pageRequest.getOrder());
        OrderBy orderBy = new OrderBy(orderByField, isAsc);

        Pager<TsysDep> tsysDepPager = sysDepBusiness
                .getPager(pageRequest.getStart(), pageRequest.getPageSize(), tsysdep, orderBy);
        List<SysDepDto> rows = new ArrayList<SysDepDto>();
        for(TsysDep dep : tsysDepPager.getRecords()){
        	rows.add(BeanUtil.copyProperties(SysDepDto.class, dep));
        }
        PageResponse rsp = PageResponseAdapter.transform(tsysDepPager);
        rsp.setRows(rows);
        return rsp;
    }

    public List<SysDepDto> getSysDepList(SysDepDto sysDepDto) {
        if (sysDepDto == null) {
            sysDepDto = new SysDepDto();
        }
        TsysDep tsysdep = BeanUtil.copyProperties(TsysDep.class, sysDepDto);
        List lsRet = sysDepBusiness.getList(tsysdep);
        List<SysDepDto> lsDep = new ArrayList<SysDepDto>(lsRet.size());
        for (Object objDep : lsRet) {
			lsDep.add(BeanUtil.copyProperties(SysDepDto.class, objDep));
		}
        return lsDep;
    }

    public boolean checkSysDepExists(SysDepDto sysDepDto) {
        TsysDep tsysdep = BeanUtil.copyProperties(TsysDep.class, sysDepDto);
        return sysDepBusiness.checkExists(tsysdep);
    }

    public List<TreeDto> getDepTreeData(SysDepDto depDto) {
    	TsysDep dep = BeanUtil.copyProperties(TsysDep.class, depDto);
        List<TsysDep> lsDep = sysDepBusiness.getTreeData(dep);
        List<TreeDto> lsTree = new ArrayList<TreeDto>();
        for (TsysDep tsysDep : lsDep) {
          TreeDto tree = new TreeDto();
          tree.setId(tsysDep.getDepCode());
          tree.setName(tsysDep.getDepName());
          tree.setpId(tsysDep.getParentCode());
		  lsTree.add(tree );			
		}
        return lsTree;
    }

    public List<TreeDto> getDepTreeByBranch(SysDepDto depDto) {
    	TsysDep dep = BeanUtil.copyProperties(TsysDep.class, depDto);
        List<TsysDep> lsDep = sysDepBusiness.getDepTreeByBranch(dep);
        List<TreeDto> lsTree = new ArrayList<TreeDto>();
        for (TsysDep tsysDep : lsDep) {
          TreeDto tree = new TreeDto();
          tree.setId(tsysDep.getDepCode());
          tree.setName(tsysDep.getDepName());
          tree.setpId(tsysDep.getParentCode());
		  lsTree.add(tree );			
		}
        return lsTree;

    }

    public List<TreeDto> getParentDepTreeByBranch(SysDepDto depDto) {
        return getDepTreeData(depDto);
        
    }
}
